function editAdminSkillCat(skillcatId){
	//get the current course title
	data = "scid="+encodeURIComponent(skillcatId);
	XMLRequestPostQ("skills/adminservices.php?method=getskillcat", data, editAdminSkillCatCallback);
}
	
function editAdminSkillCatCallback(xmlDoc){

	if(!xmlDoc){
		return;
	}
	
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var skillCatId = idNode[0].firstChild.nodeValue;

	var scNameNode = xmlDoc.documentElement.getElementsByTagName('scname');
	var scName = scNameNode[0].firstChild.nodeValue;

	// get the coursetitle field and change to be an input field
	var cell = document.getElementById('skillcatrow'+skillCatId+'edit');
	cell.innerHTML = "";

	var inputField = document.createElement("input");
	inputField.setAttribute("name","edit"+skillCatId);
	inputField.setAttribute("id","edit"+skillCatId);
	inputField.setAttribute("type","text");
	inputField.setAttribute("size","40");
	inputField.setAttribute("value",scName);
	cell.appendChild(inputField);

	var saveBtn = document.createElement("input");
	saveBtn.setAttribute("type","button");
	saveBtn.setAttribute("value","Save");
	if(saveBtn.addEventListener){ // Mozilla, Netscape, Firefox
		saveBtn.addEventListener('click', function(){saveAdminSkillCat(skillCatId);}, false);
	} else { // IE
		saveBtn.attachEvent('onclick', function(){saveAdminSkillCat(skillCatId);});
	}
	cell.appendChild(saveBtn);
}

function saveAdminSkillCat(scId){
 	var data = "scid=" + encodeURIComponent(scId);
	
	//get the name from form
	var scName = document.getElementById('edit' + scId).value;
	data += "&scname=" + encodeURIComponent(scName);
	
	XMLRequestPostQ("skills/adminservices.php?method=editskillcat", data, saveAdminSkillCatCallback);
}

function saveAdminSkillCatCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	
	// extract the Id
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var scId = idNode[0].firstChild.nodeValue;
	
	// extract the Id
	var nameNode = xmlDoc.documentElement.getElementsByTagName('scname');
	var scName = nameNode[0].firstChild.nodeValue;
	
	// extract whether or not edited
	var editedNode = xmlDoc.documentElement.getElementsByTagName('edited');
	var edited = editedNode[0].firstChild.nodeValue;
	if(edited != "true"){
		var errorNode = xmlDoc.documentElement.getElementsByTagName('error');
		var error = errorNode[0].firstChild.nodeValue;
		alert("Error: " + error);
		return;
	};
	
	var cell = document.getElementById('skillcatrow'+scId+'edit');
	cell.innerHTML = scName;
}

function deleteAdminSkillCat(scId,scName){
	if (confirm("Are you sure you want to delete the category '"+scName+"'?")){
		XMLRequestQ("skills/adminservices.php?method=deleteskillcat&scid=" + scId, deleteAdminSkillCatCallback);
	}
}

function deleteAdminSkillCatCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	// extract the Id
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var scId = idNode[0].firstChild.nodeValue;
	
	// extract whether or not deleted
	var deletedNode = xmlDoc.documentElement.getElementsByTagName('deleted');
	var deleted = deletedNode[0].firstChild.nodeValue;
	
	if(deleted != "true"){
		var errorNode = xmlDoc.documentElement.getElementsByTagName('error');
		var error = errorNode[0].firstChild.nodeValue;
		alert("Error: " + error);
		return;
	}
	//remove the node
	var row = document.getElementById('skillcatrow'+scId);
	row.parentNode.removeChild(row);
}


function addAdminSkillCat(){

	//get the date from form
	var scName = document.getElementById('skillcatname').value;
	var data = "scname=" + encodeURIComponent(scName);
	
	if (scName.trim() == ''){
		alert("You must enter a category");
		return;
	}
	XMLRequestPostQ("skills/adminservices.php?method=addskillcat", data, addAdminSkillCatCallback);
	
}

function addAdminSkillCatCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	//clear course name box
	document.getElementById('skillcatname').value = "";

	//get title & date & id from xmlDoc and add to 
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var scId = idNode[0].firstChild.nodeValue;
	
	var scNode = xmlDoc.documentElement.getElementsByTagName('scname');
	var scName = scNode[0].firstChild.nodeValue;
	
	var cRow = document.createElement("tr");
	cRow.setAttribute("id","skillcatrow"+scId);
	
	var nameF = document.createElement("td");
	nameF.setAttribute("id","skillcatrow"+scId+"edit");
	nameF.appendChild(document.createTextNode(scName));
	cRow.appendChild(nameF);
	
	var optionF = document.createElement("td");
	optionF.setAttribute("id","skillcatrow"+scId+"options");
	
	var editSpan = document.createElement("span");
	editSpan.setAttribute("id","editspan"+scId);
	if(editSpan.addEventListener){ // Mozilla, Netscape, Firefox
		editSpan.addEventListener('click', function(){
			editAdminSkillCat(scId);
			}, false);
	} else { // IE
		editSpan.attachEvent('onclick', function(){
			editAdminSkillCat(scId);
			});
	}
	changeNodeClass(editSpan,'remove');
	editSpan.appendChild(document.createTextNode("[edit]"));
	optionF.appendChild(editSpan);
	
	var deleteSpan = document.createElement("span");
	if(deleteSpan.addEventListener){ // Mozilla, Netscape, Firefox
		deleteSpan.addEventListener('click', function(){deleteAdminSkillCat(scId,scName);}, false);
	} else { // IE
		deleteSpan.attachEvent('onclick', function(){deleteAdminSkillCat(scId,scName);});
	}
	changeNodeClass(deleteSpan,'remove');
	deleteSpan.appendChild(document.createTextNode("[delete]"));
	optionF.appendChild(deleteSpan);
	
	cRow.appendChild(optionF);
	
	var brother = document.getElementById("addskillcatrow");
	brother.parentNode.insertBefore(cRow,brother);
	
}

function removeAdminSkillSkillCat(skillId,scId){
	if (confirm("Are you sure you want to remove this category ?")){
		XMLRequestQ("skills/adminservices.php?method=removeskillskillcat&scid=" + scId +"&skillid="+skillId, removeAdminSkillSkillCatCallback);
	}
}

function removeAdminSkillSkillCatCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	// extract the Id
	var scidNode = xmlDoc.documentElement.getElementsByTagName('scid');
	var scId = scidNode[0].firstChild.nodeValue;
	
	var skillidNode = xmlDoc.documentElement.getElementsByTagName('skillid');
	var skillId = skillidNode[0].firstChild.nodeValue;
	
	// extract whether or not deleted
	var deletedNode = xmlDoc.documentElement.getElementsByTagName('deleted');
	var deleted = deletedNode[0].firstChild.nodeValue;
	
	if(deleted != "true"){
		var errorNode = xmlDoc.documentElement.getElementsByTagName('error');
		var error = errorNode[0].firstChild.nodeValue;
		alert("Error: " + error);
		return;
	}
	//remove the node
	var row = document.getElementById('skillskillcat_'+skillId+'_'+scId);
	row.parentNode.removeChild(row);
}


function addAdminSkillSkillCat(skillId){

	var data = "skillid="+encodeURIComponent(skillId);
	//get the date from form
	var scIdNode = document.getElementById('skillcat'+skillId);
	var scid = scIdNode.options[scIdNode.selectedIndex].value;
	data += "&scid=" + encodeURIComponent(scid);

	XMLRequestPostQ("skills/adminservices.php?method=addskillskillcat", data, addAdminSkillSkillCatCallback);
	
}

function addAdminSkillSkillCatCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}

	//get title & date & id from xmlDoc and add to 
	var scidNode = xmlDoc.documentElement.getElementsByTagName('sc_id');
	var scId = scidNode[0].firstChild.nodeValue;

	var skillIdNode = xmlDoc.documentElement.getElementsByTagName('skill_id');
	var skillId = skillIdNode[0].firstChild.nodeValue;

	var scNode = xmlDoc.documentElement.getElementsByTagName('scname');
	var scName = scNode[0].firstChild.nodeValue;

	var catli = document.createElement("li");
	catli.setAttribute("id","skillskillcat_"+skillId+"_"+scId);
	catli.appendChild(document.createTextNode(scName+" "));
		
	var deleteSpan = document.createElement("span");
	if(deleteSpan.addEventListener){ // Mozilla, Netscape, Firefox
		deleteSpan.addEventListener('click', function(){removeAdminSkillSkillCat(skillId,scId);}, false);
	} else { // IE
		deleteSpan.attachEvent('onclick', function(){removeAdminSkillSkillCat(skillId,scId);});
	}
	changeNodeClass(deleteSpan,'remove');
	deleteSpan.appendChild(document.createTextNode("[remove]"));
	catli.appendChild(deleteSpan);

	var catul = document.getElementById("skillcatlist"+skillId);
	catul.appendChild(catli);
	
}

function editAdminSkill(skillId){
	//get the current course title
	var data = "skillid="+encodeURIComponent(skillId);
	XMLRequestQ("skills/adminservices.php?method=getskill&"+data, editAdminSkillCallback);
}
	
function editAdminSkillCallback(xmlDoc){

	if(!xmlDoc){
		return;
	}
	
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var skillId = idNode[0].firstChild.nodeValue;

	var skillNameNode = xmlDoc.documentElement.getElementsByTagName('skillname');
	var skillName = skillNameNode[0].firstChild.nodeValue;

	var skillURLNode = xmlDoc.documentElement.getElementsByTagName('skillurl');
	if (skillURLNode[0].firstChild){
		var skillURL = skillURLNode[0].firstChild.nodeValue;
	} else {
		var skillURL = "";
	}

	// get the coursetitle field and change to be an input field
	var cell = document.getElementById('skillrow'+skillId+'edit');
	cell.innerHTML = "";
	
	var txt = document.createTextNode("Name:");
	cell.appendChild(txt);
	
	var inputField = document.createElement("input");
	inputField.setAttribute("name","edit"+skillId);
	inputField.setAttribute("id","edit"+skillId);
	inputField.setAttribute("type","text");
	inputField.setAttribute("size","40");
	inputField.setAttribute("value",skillName);
	cell.appendChild(inputField);
	
	var br = document.createElement("br");
	cell.appendChild(br);
	
	var txt = document.createTextNode("URL:");
	cell.appendChild(txt);
	
	var inputField = document.createElement("input");
	inputField.setAttribute("name","editurl"+skillId);
	inputField.setAttribute("id","editurl"+skillId);
	inputField.setAttribute("type","text");
	inputField.setAttribute("size","40");
	inputField.setAttribute("value",skillURL);
	cell.appendChild(inputField);
	
	var br = document.createElement("br");
	cell.appendChild(br);
	var saveBtn = document.createElement("input");
	saveBtn.setAttribute("type","button");
	saveBtn.setAttribute("value","Save");
	if(saveBtn.addEventListener){ // Mozilla, Netscape, Firefox
		saveBtn.addEventListener('click', function(){saveAdminSkill(skillId);}, false);
	} else { // IE
		saveBtn.attachEvent('onclick', function(){saveAdminSkill(skillId);});
	}
	cell.appendChild(saveBtn);
}

function saveAdminSkill(skillId){
 	var data = "skillid=" + encodeURIComponent(skillId);
	
	//get the name from form
	var skillName = document.getElementById('edit' + skillId).value;
	data += "&skillname=" + encodeURIComponent(skillName);
	
	var skillURL = document.getElementById('editurl' + skillId).value;
	data += "&skillurl=" + encodeURIComponent(skillURL);
	XMLRequestPostQ("skills/adminservices.php?method=editskill", data, saveAdminSkillCallback);
}

function saveAdminSkillCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	
	// extract the Id
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var skillId = idNode[0].firstChild.nodeValue;
	
	// extract the Id
	var nameNode = xmlDoc.documentElement.getElementsByTagName('skillname');
	var skillName = nameNode[0].firstChild.nodeValue;
	
	var urlNode = xmlDoc.documentElement.getElementsByTagName('skillurl');
	if (urlNode[0].firstChild){
		var skillURL = urlNode[0].firstChild.nodeValue;
	} else {
		var skillURL = "";
	}
	
	// extract whether or not edited
	var editedNode = xmlDoc.documentElement.getElementsByTagName('edited');
	var edited = editedNode[0].firstChild.nodeValue;
	if(edited != "true"){
		var errorNode = xmlDoc.documentElement.getElementsByTagName('error');
		var error = errorNode[0].firstChild.nodeValue;
		alert("Error: " + error);
		return;
	};
	
	var cell = document.getElementById('skillrow'+skillId+'edit');
	cell.innerHTML = skillName + "<br/><a target='new' href='"+skillURL+"'>"+skillURL+"</a>";
}

function deleteAdminSkill(skillId,skillName){
	if (confirm("Are you sure you want to delete the skill '"+skillName+"'?")){
		XMLRequestQ("skills/adminservices.php?method=deleteskill&skillid=" + skillId, deleteAdminSkillCallback);
	}
}

function deleteAdminSkillCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	// extract the Id
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var skillId = idNode[0].firstChild.nodeValue;
	
	// extract whether or not deleted
	var deletedNode = xmlDoc.documentElement.getElementsByTagName('deleted');
	var deleted = deletedNode[0].firstChild.nodeValue;
	
	if(deleted != "true"){
		var errorNode = xmlDoc.documentElement.getElementsByTagName('error');
		var error = errorNode[0].firstChild.nodeValue;
		alert("Error: " + error);
		return;
	}
	//remove the node
	var row = document.getElementById('skillrow'+skillId);
	row.parentNode.removeChild(row);
}

function addAdminSkill(){

	//get the date from form
	var skillName = document.getElementById('skillname').value;
	var data = "skillname=" + encodeURIComponent(skillName);
	
	if (skillName.trim() == ''){
		alert("You must enter a skill");
		return;
	}
	XMLRequestPostQ("skills/adminservices.php?method=addskill", data, addAdminSkillCallback);
	
}

function addAdminSkillCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	//clear course name box
	document.getElementById('skillname').value = "";

	//get title & date & id from xmlDoc and add to 
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var skillId = idNode[0].firstChild.nodeValue;
	
	var skillNode = xmlDoc.documentElement.getElementsByTagName('skillname');
	var skillName = skillNode[0].firstChild.nodeValue;
	
	var cRow = document.createElement("tr");
	cRow.setAttribute("id","skillrow"+skillId);
	
	var nameF = document.createElement("td");
	nameF.setAttribute("id","skillrow"+skillId+"edit");
	nameF.appendChild(document.createTextNode(skillName));
	cRow.appendChild(nameF);
	
	var catF = document.createElement("td");
	var catul = document.createElement("ul");
	catul.setAttribute("id","skillcatlist"+skillId);
	changeNodeClass(catul,"skillcategorylist");
	catF.appendChild(catul);
	cRow.appendChild(catF);
	
	var optionF = document.createElement("td");
	optionF.setAttribute("id","skillrow"+skillId+"options");
	
	var editSpan = document.createElement("span");
	editSpan.setAttribute("id","editspan"+skillId);
	if(editSpan.addEventListener){ // Mozilla, Netscape, Firefox
		editSpan.addEventListener('click', function(){
			editAdminSkill(skillId);
			}, false);
	} else { // IE
		editSpan.attachEvent('onclick', function(){
			editAdminSkill(skillId);
			});
	}
	changeNodeClass(editSpan,'remove');
	editSpan.appendChild(document.createTextNode("[edit]"));
	optionF.appendChild(editSpan);
	
	var deleteSpan = document.createElement("span");
	if(deleteSpan.addEventListener){ // Mozilla, Netscape, Firefox
		deleteSpan.addEventListener('click', function(){deleteAdminSkill(skillId,skillName);}, false);
	} else { // IE
		deleteSpan.attachEvent('onclick', function(){deleteAdminSkill(skillId,skillName);});
	}
	changeNodeClass(deleteSpan,'remove');
	deleteSpan.appendChild(document.createTextNode("[delete]"));
	optionF.appendChild(deleteSpan);
	
	cRow.appendChild(optionF);
	
	var brother = document.getElementById("addskillrow");
	brother.parentNode.insertBefore(cRow,brother);
	
	//now load categories for skill
	loadCategories(skillId);
	
}

function loadCategories(skillId){
	//get the date from form
	var data = "skillid=" + encodeURIComponent(skillId);
	XMLRequestPostQ("skills/adminservices.php?method=getcategories", data, loadCategoriesCallback);
}

function loadCategoriesCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	
	var idNode = xmlDoc.documentElement.getElementsByTagName('skillid');
	var skillId = idNode[0].firstChild.nodeValue;
	var sBox = document.createElement("select");
	sBox.setAttribute("id","skillcat"+skillId);
	sBox.setAttribute("name","skillcat"+skillId);
	//loop through categories
	var catsNodes = xmlDoc.documentElement.getElementsByTagName('categories');
	var catNodes = catsNodes[0].getElementsByTagName('node');
	
	for (var i=0; i<catNodes.length; i++){
		var oValNode = catNodes[i].getElementsByTagName('ident');
		var oVal = oValNode[0].firstChild.nodeValue;
		
		var oNameNode = catNodes[i].getElementsByTagName('scname');
		var oName = oNameNode[0].firstChild.nodeValue;
		
		var sOpt = document.createElement("option");
		sOpt.setAttribute("value", oVal);
		sOpt.appendChild(document.createTextNode(oName));
		sBox.appendChild(sOpt);
	}
	var ulEle = document.getElementById("skillcatlist"+skillId);
	ulEle.parentNode.appendChild(sBox);
	
	var addB = document.createElement("input");
	addB.setAttribute("value","Add");
	addB.setAttribute("type","button");
	if(addB.addEventListener){ // Mozilla, Netscape, Firefox
		addB.addEventListener('click', function(){addAdminSkillSkillCat(skillId);}, false);
	} else { // IE
		addB.attachEvent('onclick', function(){addAdminSkillSkillCat(skillId);});
	}
	ulEle.parentNode.appendChild(addB);
}

function getCategories(skillId){
	// delete the add cat link
	var spanE = document.getElementById("addCatSpan"+skillId);
	spanE.parentNode.removeChild(spanE);
	//now load cats
	loadCategories(skillId);
}